<?php
/**
 * CreateTime: 2024/1/16 14:53
 * User:LinQ
 */

namespace App\Http\Service\Address;

use App\Func\Customer\CustomerFunc;
use App\Http\Service\BasicInterfaceService;
use App\Models\Address\AddressModel;
use Illuminate\Support\Facades\DB;

class AddressService implements BasicInterfaceService
{
    public function getCurrentAddress()
    {
        $res = (new CustomerFunc())->getLoginInfo();
        // 获取我的地址下拉
        $address = (new AddressModel())->where('customer_id',$res['customer_id'])
            ->where('status',1)
            ->get();
        s_c_return($address,trans('message.get_list_ok',[
            'name' => trans('key.address')
        ]));
    }

    public function getList(array $prams)
    {
        
    }

    public function dispose($data): mixed
    {

    }

    public function listSelectFilters($query, $filters)
    {

    }

    public function getSelect()
    {
        
    }

    public function getSelectHasPage($prams)
    {
        
    }

    public function getInfo($id)
    {
        
    }

    public function create($data)
    {
        // 查看是否存在设置默认值
        $addressModel = new AddressModel();
        $info = (new CustomerFunc())->getLoginInfo();
        DB::beginTransaction();
        $data['customer_id'] = $info['customer_id'];
        $data['created_at'] = date('Y-m-d h:i:s');
        $data['status'] = 1;
        try {
            // 查询当前人拥有的所有地址
            $addressIds = $addressModel->select('address_id')->where('customer_id',$info['customer_id'])->where('status',1)->get();
            $fmtAddressIds = array_column($addressIds->toArray(),'address_id');
            if(count($fmtAddressIds)>=10){
                s_fail_return(null,trans('message.created_err',[
                    'name' => trans('key.address')
                ]).":".trans('message.g_t_max_count',[
                    'count' => 10
                ]));
            }
            if(isset($data['is_default'])&&$data['is_default']==1){
                // 先替换所有的地址为非默认
                $addressModel->whereIn('address_id',$fmtAddressIds)->update([
                    'is_default' => 2
                ]);
            }
            $id = $addressModel->insertGetId($data);
            s_c_return([
                'address_id' => $id
            ],trans('message.created_ok',[
                'name' => trans('key.address')
            ]));
            DB::commit();
        }catch (\Exception $exception){
            DB::rollBack();
            s_fail_return(null,trans('message.created_err',[
                'name' => trans('key.address')
            ]));
        }
    }

    public function setDefaultAddress($addressId)
    {
        // 查看是否存在设置默认值
        $addressModel = new AddressModel();
        $info = (new CustomerFunc())->getLoginInfo();
        $addressId = $addressModel
            ->select('address_id')
            ->where('address_id',$addressId)
            ->where('customer_id',$info['customer_id'])
            ->first();
        if(!$addressId){
            s_fail_return(null,trans('message.update_err',[
                'name' => trans('key.address')
            ]));
        }
        // 查询当前人拥有的所有地址
        $addressIds = $addressModel->select('address_id')->where('status',1)->where('customer_id',$info['customer_id'])->get();
        $fmtAddressIds = array_column($addressIds->toArray(),'address_id');
        $addressModel->whereIn('address_id',$fmtAddressIds)->update([
            'is_default' => 2
        ]);
        $addressModel->where('address_id',$addressId['address_id'])->update([
            'is_default' => 1
        ]);
        s_c_return(null,trans('message.update_ok',[
            'name' => trans('key.address')
        ]));
    }
    public function fakeDelete($id)
    {
        $info = (new CustomerFunc())->getLoginInfo();
        $addressModel = new AddressModel();
        $addressInfo = $addressModel
            ->select('address_id','is_default')
            ->where('address_id',$id)
            ->where('customer_id',$info['customer_id'])
            ->first();
        if(!$addressInfo){
            s_fail_return(null,trans('message.update_err',[
                'name' => trans('key.address')
            ]));
        }
        if($addressInfo['is_default']==1){
            s_fail_return(null,trans('message.update_err',[
                'name' => trans('key.address')
            ]).":".trans('message.default_not_delete',[
                'name' => trans('key.address')
            ]));
        }
        $addressModel->where('address_id',$id)->update([
            'status' => 2
        ]);
        s_c_return(null,trans('message.update_ok',[
            'name' => trans('key.address')
        ]));

    }

    public function fakeDeleteByIds($ids)
    {
        
    }

    public function delete($id)
    {
        
    }

    public function update($data)
    {
        $info = (new CustomerFunc())->getLoginInfo();
        $addressModel = new AddressModel();
        $addressInfo = $addressModel
            ->select('address_id')
            ->where('address_id',$data['customer_id'])
            ->where('customer_id',$info['customer_id'])
            ->first();
        if(!$addressInfo){
            s_fail_return(null,trans('message.update_err',[
                'name' => trans('key.address')
            ]));
        }
        $updateData = [];
        if(isset($data['recipient_name'])&&$data['recipient_name']){
            $updateData['recipient_name'] = $data['recipient_name'];
        }
        if(isset($data['phone_number'])&&$data['phone_number']){
            $updateData['phone_number'] = $data['phone_number'];
        }
        if(isset($data['city'])&&$data['city']){
            $updateData['city'] = $data['city'];
        }
        if(isset($data['state'])&&$data['state']){
            $updateData['state'] = $data['state'];
        }
        if(isset($data['address_line'])&&$data['address_line']){
            $updateData['address_line'] = $data['address_line'];
        }
        if(isset($data['postal_code'])&&$data['postal_code']){
            $updateData['postal_code'] = $data['postal_code'];
        }
        $addressModel->where('address_id',$data['customer_id'])->update($updateData);
        s_c_return(null,trans('message.update_ok',[
            'name' => trans('key.address')
        ]));
    }

}